/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package projetCalendar.controllers.evenements;

import java.io.*;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.servlet.*;
import javax.servlet.http.*;
import projetCalendar.Model.Evenement;


public class SupprimerEvenementController extends HttpServlet {
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("ProjetCalendarPU");
    private EntityManager manager = emf.createEntityManager();
    private EntityTransaction utx;
   
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String id_str = request.getParameter("id");
        Long id = Long.parseLong(id_str);
        String retour = request.getParameter("retour");
        try {
            utx = manager.getTransaction();
            utx.begin();
        } catch(Exception e) { }
        Evenement suppEvent = manager.find(Evenement.class, id);
        if(id == null || suppEvent == null) {
            if(!isAjax(request)) {
                response.sendError(HttpServletResponse.SC_NOT_FOUND);
            } else {
                request.setAttribute("id", id_str);
                getServletContext().getRequestDispatcher("/WEB-INF/vues/evenements/supprimer_ko.js.jsp").forward(request, response);
            }
        } else {
            try {
                manager.remove(suppEvent);
                utx.commit();
                if (!isAjax(request)) {
                    response.sendRedirect(retour);
                } else {
                    request.setAttribute("id", id_str);
                    getServletContext().getRequestDispatcher("/WEB-INF/vues/evenements/supprimer_ok.js.jsp").forward(request, response);
                }
            } catch (Exception ex) {
                System.out.println("oops");
            }
        }  
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    @Override public void destroy() {
        emf.close();
    }
    
    private boolean isAjax(HttpServletRequest request) {
        String xrequestedwith = request.getHeader("X-Requested-With");
        return (xrequestedwith != null);
    }

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
